//
//  HXTabBarViewController.m
//  KD
//
//  Created by Hubery on 16/1/29.
//  Copyright © 2016年 ifly. All rights reserved.
//

#import "HXTabBarViewController.h"
#import "HXNavigationController.h"
#import "HXSelectedViewController.h"
#import "HXInformViewController.h"
#import "HXApplyViewController.h"
#import "HXPersonListTableViewController.h"
#import "HXIflyAddressBookTableViewController.h"
#import "HXCompanyInformTableViewController.h"

@interface HXTabBarViewController ()<UITabBarDelegate,UITabBarControllerDelegate>
//@property(nonatomic,strong) NSMutableArray *array;
@end

@implementation HXTabBarViewController

static id _instace;

////遗留问题
//+ (instancetype)sharedTabBar
//{
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        _instace = [[self alloc] init];
//    });
//    return _instace;
//}
//
//+ (id)allocWithZone:(struct _NSZone *)zone
//{
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        _instace = [super allocWithZone:zone];
//    });
//    return _instace;
//}

//
//
//- (id)copyWithZone:(NSZone *)zone
//{
//    return _instace;
//}
//进行极光登录
-(void)loadLogInJGPush:(NSTimer *)timer{
    if ([[UserDefaultHandler getUserDetault:kIsLoginJpush] isEqualToString:@"yes"]) {
        [timer invalidate];
        timer = nil;
        return;
    }
    [[[HXSelectedViewController alloc] init] loadCreateNewLoginJPush];
}
//进行极光推送登录
-(void)loadCreateLoginJPush:(NSTimer *)timer{
    if ([[UserDefaultHandler getUserDetault:kIResCodeIs0Jpush] isEqualToString:@"yes"]) {
        [timer invalidate];
        timer = nil;
        return;
    }
    [[[HXSelectedViewController alloc] init] loadCreateJPushWithAlias];
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    //  0. 进行极光及极光推送登录
    if (![[UserDefaultHandler getUserDetault:kIsLoginJpush] isEqualToString:@"yes"]) {//极光没有登录成功
        //        [[[HXSelectedViewController alloc] init] loadCreateNewLoginJPush];
        [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(loadLogInJGPush:) userInfo:nil repeats:NO];
    }else{//极光登录成功
        if (![[UserDefaultHandler getUserDetault:kIResCodeIs0Jpush] isEqualToString:@"yes"]) {//极光推送未登录成功
            [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(loadCreateLoginJPush:) userInfo:nil repeats:NO];
        }
    }
//    self.delegate=self;
    self.delegate=self;
//    self.array = [[NSMutableArray alloc]init];
    
}

-(void)jumpToMe{
    self.selectedIndex = 0;
}

//添加所有子控制器
-(void)setUpAllChildViewController
{
    //通知
    if ([[UserDefaultHandler getUserDetault:Users_IsPayVip] boolValue] == YES) {
        
        UIStoryboard *informSb = [UIStoryboard storyboardWithName:@"HXInform" bundle:nil];
        HXCompanyInformTableViewController *informVc = [informSb instantiateViewControllerWithIdentifier:@"HXCompanyInformTableViewController"];
        [self setUpOneChildViewController:informVc image:[UIImage imageWithOriginalName:@"tabBar_1"] selectedName:[UIImage imageWithOriginalName:@"tabBar_high_1"] title:@"通知"];
        
    }else{
    
        UIStoryboard *informSb = [UIStoryboard storyboardWithName:@"HXInform" bundle:nil];
        HXInformViewController *informVc = [informSb instantiateViewControllerWithIdentifier:@"HXInformViewController"];
        [self setUpOneChildViewController:informVc image:[UIImage imageWithOriginalName:@"tabBar_1"] selectedName:[UIImage imageWithOriginalName:@"tabBar_high_1"] title:@"通知"];
    }
    
    //应用
    UIStoryboard *applySb = [UIStoryboard storyboardWithName:@"Apply" bundle:nil];
    HXApplyViewController *applyVc = [applySb instantiateViewControllerWithIdentifier:@"HXApplyViewController"];
    [self setUpOneChildViewController:applyVc image:[UIImage imageWithOriginalName:@"tabBar_2"] selectedName:[UIImage imageWithOriginalName:@"tabBar_high_2"] title:@"应用"];
    
    if ([_userKind integerValue] == UserTypeManage) {//是主管
        //通讯录
        HXPersonListTableViewController *personListVc = [[HXPersonListTableViewController alloc]init];
        [self setUpOneChildViewController:personListVc image:[UIImage imageWithOriginalName:@"tabBar_4"] selectedName:[UIImage imageWithOriginalName:@"tabBar_high_4"] title:@"通讯录"];
        
    }else if([[UserDefaultHandler getUserDetault:kUsers_Kind] integerValue] == UserTypeCorp){//是企业
        if ([[UserDefaultHandler getUserDetault:Users_IsPayVip]integerValue]){
            HXIflyAddressBookTableViewController *IflyAddressBook = [[HXIflyAddressBookTableViewController alloc] init];
            [self setUpOneChildViewController:IflyAddressBook image:[UIImage imageWithOriginalName:@"tabBar_4"] selectedName:[UIImage imageWithOriginalName:@"tabBar_high_4"] title:@"通讯录"];
    }
    }else if ([[UserDefaultHandler getUserDetault:kUsers_Kind] integerValue] == UserTypeBank){//是银行
    }else{
    }
    
    //我
    UIStoryboard *mesb = [UIStoryboard storyboardWithName:@"Me" bundle:nil];
    HXInformViewController *meVc = [mesb instantiateViewControllerWithIdentifier:@"HXMeViewController"];
    [self setUpOneChildViewController:meVc image:[UIImage imageWithOriginalName:@"tabBar_3"] selectedName:[UIImage imageWithOriginalName:@"tabBar_high_3"] title:@"我"];
    
}

#pragma mark - 添加一个子控制器
-(void) setUpOneChildViewController:(UIViewController *)vc image:(UIImage *)image  selectedName:(UIImage *)selectedImage title:(NSString *)title
{
    vc.tabBarItem.image = image;
    vc.tabBarItem.selectedImage = selectedImage;
    vc.title = title;
    HXNavigationController *navi = [[HXNavigationController alloc]initWithRootViewController:vc];
    [self addChildViewController:navi];
//    [self.array addObject:navi];
}

// 判断是否是空值，空值返回YES，否则返回NO
- (BOOL)isNilValue:(NSString *)value {
    if (value == nil || value.length <= 0 || [value isEqualToString:@""] || [value isEqualToString:@"(null)"]) {
        return YES;
    } else {
        return NO;
    }
}

+(void)initialize
{
    UITabBarItem *tem = [UITabBarItem appearanceWhenContainedIn:self, nil];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSMutableDictionary *dictNor = [NSMutableDictionary dictionary];
    [dict setObject:MAINCOLOR forKey:NSForegroundColorAttributeName];
    [tem setTitleTextAttributes:dict forState:UIControlStateSelected];
    [dictNor setObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName];
    [tem setTitleTextAttributes:dictNor forState:UIControlStateNormal];
}

-(void)setUserKind:(NSString *)userKind {
    _userKind = userKind;
//    [self.array removeAllObjects];
    [self setUpAllChildViewController];
}

-(void)setEditionTabBarStr:(NSString *)editionTabBarStr {
    
    _editionTabBarStr = editionTabBarStr;
}





@end
